#coding=utf-8

'''
 * 字符串全角半角转换操作类
 * 版权所有
 * @author      xuxb<xuxiaobovip@126.com>
 * @version     $Id: fullHalfConversion.py $
'''

class fullHalfConversion:
    
    #构造函数
    def __ini__(self):
        pass
    
    #将半角字符转换成全角字符
    def charH2F(self, uchar):

        if uchar == '':
            return ''
        
        inside_code = ord(uchar)

        if not inside_code in range(32, 127):
            return uchar
        
        if inside_code == 32:
            inside_code = 12288
        else:
            inside_code += 65248

        if inside_code in range(32, 127):
            return uchar
        
        return unichr(inside_code).encode('utf-8', 'ignore')

    #将全角字符转换成半角字符
    def charF2H(self, uchar):

        if uchar == '':
            return ''
        
        inside_code = ord(uchar)

        if inside_code in range(32, 127):
            return uchar

        if inside_code == 12288:
            inside_code = 32
        else:
            inside_code -= 65248

        if not inside_code in range(32, 127):
            return uchar
        
        return unichr(inside_code).encode('utf-8', 'ignore')

    #将字符串中的半角转换成全角
    def StringH2F(self, ustring):
        
        try:
            ustring = ustring.decode('utf-8', 'ignore')
        except:
            pass
        
        result = ''
        for uchar in ustring:
            
            try:
                uchar = uchar.encode('utf-8', 'ignore')
            except:
                pass
            
            try:
                inside_code = ord(uchar)
            except:
                inside_code = ''
            
            if inside_code:
                if inside_code in range(32, 127):
                    result += self.charH2F(uchar)
                else:
                    result += uchar
            else:
                result += uchar

        return result

    #将字符串中的全角转换成半角
    def StringF2H(self, ustring):
        
        try:
            ustring = ustring.decode('utf-8', 'ignore')
        except:
            pass
        
        result = ''
        for uchar in ustring:

            try:
                inside_code = ord(uchar)
            except:
                inside_code = ''
            
            if inside_code:
                if inside_code in range(32, 127):
                    result += uchar
                else:
                    result += self.charF2H(uchar)
            else:
                result += uchar

        return result.encode('utf-8', 'ignore')

    #析构函数
    def __del__(self):
        pass

if __name__ == '__main__':
    fhObj = fullHalfConversion()

    str = '1234567890abcdefghijklmnopqrstuvwxyz'
    print fhObj.StringF2H(str)
    print fhObj.StringH2F(str)
